package nl.saxion.webtech.roulette.model;

public class Player {
	private int		playerId;
	private String	username;
	private String	password;
	private int		currentTableId;
	private int		cashBuyIn;
	private int		amountOfCash;
	private boolean	loggedIn;

	public Player( String username, String password ) {
		this.playerId = generateId();
		this.username = username;
		this.password = password;
		this.amountOfCash = 2000;
		this.loggedIn = false;
	}

	public int joinTable( int tableId, int cash ) {
		/*
		 * Return values 
		 *  1: Cash has been buyed in
		 * -2: Your account balance is way to low, first add more money to your account
		 * -3: Allready on table
		 */
		if( amountOfCash > 250 ) {
			currentTableId = tableId;
			Table currentTable = Lobby.getInstance().getTable( tableId );
			if( amountOfCash >= cash ) {
				for( Player player : currentTable.getPlayers() ) {
					if( player.getPlayerId() == playerId ) {
						System.out.println( "You're allready on the table" );
						return -3;
					}
				}
				cashBuyIn = cash;
				amountOfCash -= cash;
				currentTable.addPlayer( this );
				return 1;
			} else {
				System.out.println("Je hebt niet genoeg geld!");
				return -1;
			}
		} else {
			System.out.println( "Your account balance is way to low, first add more money to your account" );
			return -2;
		}
	}

	public boolean placeBet( Bet bet ) {
		if( bet.getCashAmount() > amountOfCash ) {
			System.out.println( "Bet is bigger than current amount of cash" );
			return false;
		}
		//TODO: Set bet amount to player.. because bets is cleared after each spin on the table
		
		amountOfCash -= bet.getCashAmount();
		Lobby.getInstance().getTable( currentTableId ).placeBet( bet );
		return true;
	}

//	public Table getCurrentTable() {
//		return Lobby.getInstance().getTable( currentTableId );
//	}

	public int getPlayerId() {
		return playerId;
	}

	public int getAmountOfCash() {
		return amountOfCash;
	}

	public void setAmountOfCash( int amountOfCash ) {
		this.amountOfCash = amountOfCash;
	}

	public int getCashBuyIn() {
		return cashBuyIn;
	}

	public void setCashBuyIn( int cashBuyIn ) {
		this.cashBuyIn = cashBuyIn;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername( String username ) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword( String password ) {
		this.password = password;
	}

	public boolean isLoggedIn() {
		return loggedIn;
	}

	public void setLoggedIn( boolean loggedIn ) {
		this.loggedIn = loggedIn;
	}

	public void addCash( int cash ) {
		this.amountOfCash += cash;
	}

	private int generateId() {
		return Lobby.getInstance().getPlayers().size() + 1;
	}

}